﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc。@Copy Right 2019
// 文件： Users.cs
// 项目名称：斑马车务
// 创建时间：2019-12-11
// 负责人：lsc
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using Dapper.Extensions;
using CRM.Infrastructure.Entity;
using CRM.Infrastructure.Repository;
using CRM.Infrastructure.Models;

namespace CRM.Infrastructure.Service
{

    /// <summary>
    ///用户表
    /// </summary>
    public class UsersService
    {
        protected UsersRepository repository;

        public UsersService()
        {
            repository = new UsersRepository();
        }

        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(Users entity) { return repository.Insert(entity); }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<Users> entitys) { return repository.Insert(entitys); }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(Users entity) { return repository.Update(entity); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(Users entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public Users SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }


        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public Users Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<Users> QueryAll()
        {
            return repository.QueryAll();
        }

        #endregion

        public Users GetByLoginNum(string userName)
        {
            return repository.GetByLoginNum(userName);
        }

        /// <summary>
        /// 查询用户列表
        /// 此方法仅用于管理员
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<Users> Pages(int pageIndex, int pageSize, UserSearchModel model)
        {
            return repository.Pages(pageIndex, pageSize, model);
        }

        /// <summary>
        /// 设置用户逻辑删除
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="isActvie"></param>
        public void SetIsActive(long userId, int isActvie)
        {
            repository.SetIsActive(userId, isActvie);
        }

        /// <summary>
        /// 根据公司获取用户
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public List<Users> GetByCompany(int companyId)
        {
            return repository.GetByCompany(companyId);
        }
    }
}